﻿<Window x:Class="MvvmOrm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="clr-namespace:System;assembly=mscorlib"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:l="clr-namespace:MvvmOrm"
        Title="{Binding ViewTitle}" Height="428" Width="635" Name="window" Closing="window_Closing">
    <Window.Resources>
        <s:Boolean x:Key="True">True</s:Boolean>
        <s:Boolean x:Key="False">False</s:Boolean>
        <BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
    </Window.Resources>
    <Window.InputBindings>
        <KeyBinding Command="{Binding CloseCmd}" Key="W" Modifiers="Ctrl" />
        <KeyBinding Command="{Binding OpenNewCmd}" Key="N" Modifiers="Ctrl" />
        <KeyBinding Command="{Binding OpenCmd}" Key="O" Modifiers="Ctrl" />
        <KeyBinding Command="{Binding SaveCmd}" CommandParameter="{StaticResource False}" Key="S" Modifiers="Ctrl" />
    </Window.InputBindings>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" IsMainMenu="True">
            <MenuItem Header="File">
                <MenuItem Header="New" Command="{Binding OpenNewCmd}" InputGestureText="Ctrl+N" />
                <MenuItem Header="Open..." Command="{Binding OpenCmd}" />
                <MenuItem Header="Save" Command="{Binding SaveCmd}" CommandParameter="{StaticResource False}" />
                <MenuItem Header="Save as..." Command="{Binding SaveCmd}" CommandParameter="{StaticResource True}" />
                <MenuItem Header="Close" Command="{Binding CloseCmd}" InputGestureText="Ctrl+W"  />
            </MenuItem>
        </Menu>
        <TabControl Grid.Row="1" >
            <TabItem Header="Options" >
                <TabItem.Style>
                    <Style TargetType="TabItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsOpen}" Value="False" >
                                <Setter Property="IsSelected" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TabItem.Style>
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Default save directory" Width="130" />
                        <TextBox Text="{Binding DefaultDirectorySave, Source={x:Static l:Properties.Settings.Default}}" Width="300" IsReadOnly="True" />
                        <Button Width="30" Content="..." Command="{Binding SetDefaultDirectorySaveCmd}"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="Default output directory" Width="130" />
                        <TextBox Text="{Binding DefaultDirectoryOutput, Source={x:Static l:Properties.Settings.Default}}" Width="300" IsReadOnly="True" />
                        <Button Width="30" Content="..." Command="{Binding SetDefaultDirectoryOutputCmd}"/>
                    </StackPanel>
                    <CheckBox Content="Build Assembly" IsChecked="{Binding BuildAssembly, Source={x:Static l:Properties.Settings.Default}}" />
                    <CheckBox Content="Create XML Doc" IsChecked="{Binding GenerateXmlDoc, Source={x:Static l:Properties.Settings.Default}}" />
                    <GroupBox Header="AssembyInfo.cs">
                        <TextBox AcceptsReturn="True"  Text="{Binding AssemblyInfo, Source={x:Static l:Properties.Settings.Default}}" FontFamily="Courier New" TextWrapping="NoWrap" FontSize="11" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" MinLines="15" />
                    </GroupBox>
                    <Button Content="Reset all to defaults" HorizontalAlignment="Left"  Command="{Binding ResetSettingsCmd}" />
                </StackPanel>
            </TabItem>
            <TabItem Header="Main" IsEnabled="{Binding IsOpen}">
                <TabItem.Style>
                    <Style TargetType="TabItem">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding IsOpen}" Value="True" >
                                <Setter Property="IsSelected" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </TabItem.Style>
                <Grid DataContext="{Binding DB}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <GroupBox Grid.Row="0" Header="RDBMS:">
                        <ComboBox ItemsSource="{Binding DbSystemList}" SelectedItem="{Binding Path=DbSystemEnum}" />
                    </GroupBox>
                    <GroupBox Grid.Row="1" Header="Schema detail source:">
                        <StackPanel>
                            <ContentControl Content="{Binding}">
                                <ContentControl.Resources>
                                    <DataTemplate x:Key="TemplateMysql">
                                        <Label Content="Not supported" />
                                    </DataTemplate>
                                    <DataTemplate x:Key="TemplateMssql">
                                        <StackPanel>
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="Server: " />
                                                <TextBox MinWidth="150" Text="{Binding Server}" />
                                                <TextBlock Text="Database: " />
                                                <TextBox MinWidth="150" Text="{Binding Database}" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" >
                                                <CheckBox Content="Use integrated security" IsChecked="{Binding Path=SqlServerUseIntegratedSecurity}" />
                                                <TextBlock Text="Username: " />
                                                <TextBox MinWidth="150" Text="{Binding Username}" />
                                                <TextBlock Text="Password: " />
                                                <TextBox MinWidth="150" Text="{Binding Password}" />
                                            </StackPanel>
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="Port: " />
                                                <TextBox MinWidth="150" Text="{Binding Port}" />
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                    <DataTemplate x:Key="TemplateMsaccess">
                                        <Label Content="Not supported" />
                                    </DataTemplate>
                                    <DataTemplate x:Key="TemplatePgsql">
                                        <Label Content="Not supported" />
                                    </DataTemplate>
                                    <DataTemplate x:Key="TemplateSqlite">
                                        <StackPanel>
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="Database file: " />
                                                <TextBox MinWidth="150" IsReadOnly="True" Text="{Binding Database}" />
                                                <Button Content=" ... " Command="{Binding SetDatabaseFileCmd}" />
                                            </StackPanel>
                                            <CheckBox Content="Use SQL Schema File" IsChecked="{Binding UseSchemaFile}" />
                                            <StackPanel Orientation="Horizontal" >
                                                <TextBlock Text="SQL Schema file: " />
                                                <TextBox MinWidth="150" IsReadOnly="True" Text="{Binding SchemaFile}" />
                                                <Button Content=" ... " Command="{Binding SetSchemaFileCmd}" />
                                            </StackPanel>
                                        </StackPanel>
                                    </DataTemplate>
                                </ContentControl.Resources>
                                <ContentControl.Style>
                                    <Style TargetType="ContentControl">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding DbSystem}" Value="0">
                                                <Setter Property="ContentTemplate" Value="{StaticResource TemplateMysql}"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding DbSystem}" Value="1">
                                                <Setter Property="ContentTemplate" Value="{StaticResource TemplateMssql}"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding DbSystem}" Value="2">
                                                <Setter Property="ContentTemplate" Value="{StaticResource TemplateMsaccess}"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding DbSystem}" Value="3">
                                                <Setter Property="ContentTemplate" Value="{StaticResource TemplatePgsql}"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding DbSystem}" Value="4">
                                                <Setter Property="ContentTemplate" Value="{StaticResource TemplateSqlite}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ContentControl.Style>
                            </ContentControl>
                            <Button Content="Analyse database schema" HorizontalAlignment="Left" Command="{Binding AnalyseCmd}" />
                        </StackPanel>
                    </GroupBox>
                    <GroupBox Grid.Row="2" Header="Generate models:">
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Namespace: "/>
                                <TextBox Text="{Binding Namespace}" MinWidth="200" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock Text="Output directory: " />
                                <TextBox MinWidth="150" IsReadOnly="True" Text="{Binding OutputDirectory}" />
                                <Button Content=" ... " Command="{Binding SetOutputDirectoryCmd}" />
                            </StackPanel>
                            <Button Content="Generate" HorizontalAlignment="Left" Command="{Binding GenerateCmd}" />
                        </StackPanel>
                    </GroupBox>
                    <GroupBox Grid.Row="3" Header="Logs">
                        <ListBox ItemsSource="{Binding Child_Logs}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Message}" />
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </GroupBox>
                </Grid>
            </TabItem>
            <TabItem Header="Items" IsEnabled="{Binding IsOpen}">
                <Grid DataContext="{Binding DB}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="1*" />
                        <RowDefinition Height="6" />
                        <RowDefinition Height="1*" />
                    </Grid.RowDefinitions>
                    <Grid.Resources>
                        <CollectionViewSource x:Key="GroupedDbItems" Source="{Binding Child_DatabaseItems}">
                            <CollectionViewSource.GroupDescriptions>
                                <PropertyGroupDescription PropertyName="TypeEnum" />
                            </CollectionViewSource.GroupDescriptions>
                        </CollectionViewSource>
                        <CollectionViewSource x:Key="GroupedDbItemLogs" Source="{Binding Path=Child_DatabaseItemLogs, Source={StaticResource GroupedDbItems}}">
                            <CollectionViewSource.GroupDescriptions>
                                <PropertyGroupDescription PropertyName="MessageTypeEnum" />
                            </CollectionViewSource.GroupDescriptions>
                        </CollectionViewSource>
                    </Grid.Resources>
                    <Grid Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="1*" />
                            <ColumnDefinition Width="6" />
                            <ColumnDefinition Width="1*" />
                        </Grid.ColumnDefinitions>
                        <ListView ItemsSource="{Binding Source={StaticResource GroupedDbItems}}" IsSynchronizedWithCurrentItem="True" SelectionMode="Single">
                            <ListView.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.ContainerStyle>
                                        <Style TargetType="{x:Type GroupItem}">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                                        <Expander IsExpanded="True">
                                                            <Expander.Header>
                                                                <TextBlock FontWeight="Bold">
                                                                 <Run Text="{Binding ItemCount, Mode=OneWay}"/> <Run Text="{Binding Name, Mode=OneWay}"/>(s)
                                                                </TextBlock>
                                                            </Expander.Header>
                                                            <Expander.Content>
                                                                <ItemsPresenter />
                                                            </Expander.Content>
                                                        </Expander>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </GroupStyle.ContainerStyle>
                                </GroupStyle>
                            </ListView.GroupStyle>
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                                    <GridViewColumn Header="Columns" DisplayMemberBinding="{Binding Path=Child_Columns.Count}" />
                                    <GridViewColumn Header="Include">
                                        <GridViewColumn.CellTemplate>
                                            <DataTemplate>
                                                <CheckBox IsEnabled="{Binding CanBeIncluded}" IsChecked="{Binding IsIncluded}" />
                                            </DataTemplate>
                                        </GridViewColumn.CellTemplate>
                                    </GridViewColumn>
                                </GridView>
                            </ListView.View>
                        </ListView>
                        <GridSplitter Grid.Column="1" HorizontalAlignment="Stretch" />
                        <ListBox Grid.Column="2" ItemsSource="{Binding Source={StaticResource GroupedDbItemLogs}}" SelectionMode="Single">
                            <ListBox.GroupStyle>
                                <GroupStyle>
                                    <GroupStyle.ContainerStyle>
                                        <Style TargetType="{x:Type GroupItem}">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate TargetType="{x:Type GroupItem}">
                                                        <Expander IsExpanded="True">
                                                            <Expander.Header>
                                                                <TextBlock FontWeight="Bold">
                                                                 <Run Text="{Binding ItemCount, Mode=OneWay}"/> <Run Text="{Binding Name, Mode=OneWay}"/>(s)
                                                                </TextBlock>
                                                            </Expander.Header>
                                                            <Expander.Content>
                                                                <ItemsPresenter />
                                                            </Expander.Content>
                                                        </Expander>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </GroupStyle.ContainerStyle>
                                </GroupStyle>
                            </ListBox.GroupStyle>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Message}" />
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid>
                    <GridSplitter Grid.Row="1" HorizontalAlignment="Stretch" />
                    <GroupBox Grid.Row="2" Header="Columns">
                        <ListView ItemsSource="{Binding Path=Child_Columns, Source={StaticResource GroupedDbItems}}" IsSynchronizedWithCurrentItem="True">
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                                    <GridViewColumn Header="Db Type" DisplayMemberBinding="{Binding DbTypeStringed}" />
                                    <GridViewColumn Header="Size/Max Length" DisplayMemberBinding="{Binding MaxLength}" />
                                    <GridViewColumn Header="Primary Key" DisplayMemberBinding="{Binding IsPrimaryKey}" />
                                    <GridViewColumn Header="Nullable" DisplayMemberBinding="{Binding Nullable}" />
                                    <GridViewColumn Header="Enum Values" DisplayMemberBinding="{Binding Path=Child_ColumnEnums.Count}" />
                                </GridView>
                            </ListView.View>
                        </ListView>
                    </GroupBox>
                </Grid>
            </TabItem>
        </TabControl>
    </Grid>
</Window>
